﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_TableList : DatabasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                OutlookMaster om = (OutlookMaster)Master;
                om.ContentHeader.Text = "Tables";

                gvTableList.DataSource = DBData.DatabaseObjects(ConnectionString, DatabaseObjectTypes.Tables);
                gvTableList.DataBind();
            }
            txtSearch.Focus();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }
    protected void gvTableList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            lblMessage.Text = string.Empty;
            {
                Response.Redirect(string.Format("TableContent.aspx?TableName={0}", gvTableList.SelectedValue));
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }
    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string selectStr = string.Format("SELECT * FROM INFORMATION_SCHEMA.Tables WHERE 1=1 ");


        DatabaseObjectTypes dot;
        dot = DatabaseObjectTypes.Tables;

        if (dot == DatabaseObjectTypes.Tables)
        {
            selectStr += " AND TABLE_TYPE = 'BASE TABLE' ";
        }
        else if (dot == DatabaseObjectTypes.Views)
        {
            selectStr += " AND TABLE_TYPE = 'VIEW' ";
        }

        selectStr += string.Format("AND table_name LIKE '%{0}%' ", DataAccess.ClearStringForSQL(txtSearch.Text));

        selectStr += "ORDER BY table_name";

        gvTableList.DataSource = DataAccess.GetDataTable(ConnectionString, selectStr);
        gvTableList.DataBind();

    }
}